﻿using System;
using System.IO;
using System.Text;
using LingDong.CommonUtility;
using LingDong.HtmlClassification;
using NLog;

namespace LingDong.HtmlClassificationExample
{
    class BayesTest
    {
        internal static void RunLearnTest()
        {
            BayesLearn learn = new BayesLearn();
            learn.Learn();
            learn.SaveResult();
        }

        internal static void RunClassificationTest()
        {
            BayesClassification.ReadLearnResult();
            int[,] resultNum = new int[BayesUtility.CategoryNumber, BayesUtility.CategoryNumber];
            for (int i = 0; i < BayesUtility.CategoryNumber; i++)
            {
                string[] fileList = Directory.GetFiles(testHtmlPath[i]);
                foreach (string file in fileList)
                {
                    string html = Utility.GetFileContent(file);
                    int bayesResult = BayesClassification.ClassificationHtml(html);
                    resultNum[i, bayesResult]++;
                }
            }

            // output result
            for (int i = 0; i < BayesUtility.CategoryNumber; i++)
            {
                int sum = 0;
                StringBuilder content = new System.Text.StringBuilder();
                content.Append("Category");
                content.Append(i);
                content.Append("\t");
                for (int j = 0; j < BayesUtility.CategoryNumber; j++)
                {
                    content.Append(resultNum[i, j]);
                    content.Append("\t");
                    sum += resultNum[i, j];
                }
                content.Append((double)resultNum[i, i] / sum * 100);
                content.Append("%");
                //logger.Debug(content.ToString());
                Console.WriteLine(content.ToString());
            }
        }

        internal static void OutputFeatureImpact()
        {
            BayesClassification.ReadLearnResult();
            double[] p_c = new double[BayesUtility.FeatureNumber];
            for (int i = 0; i < BayesUtility.FeatureNumber; i++)
            {
                for (int k = 0; k < BayesUtility.Dimensionality; k++)
                {
                    p_c[i] += Math.Abs(BayesClassification.LearnResult.P_f_c[i, 0, k] - 
                                       BayesClassification.LearnResult.P_f_c[i, 1, k]);
                }
                logger.Debug(p_c[i] / 10);
            }
        }

        private static string[] testHtmlPath = new string[]
        {
            @"F:\LingDongData\Classification\Test\Content\",
            @"F:\LingDongData\Classification\Test\Hub\",
        };

        private static Logger logger = LogManager.GetCurrentClassLogger();
    }
}
